<?php
/**
 * 用户个人资料视图
 */
include '/www/wwwroot/MA.com/includes/header.php';
?>

<div class="container mt-3">
    <div class="row profile-container justify-content-center">
        <div class="col-md-5">
            <div class="card mb-4">
                <div class="card-header" style="padding: var(--bs-card-cap-padding-y) var(--bs-card-cap-padding-x);">
                    <ul class="nav nav-tabs card-header-tabs" id="profileTabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="true">个人资料</button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="password-tab" data-bs-toggle="tab" data-bs-target="#password" type="button" role="tab" aria-controls="password" aria-selected="false">修改密码</button>
                        </li>
                    </ul>
                </div>
                
                <div class="card-body">
                    <div class="tab-content" id="profileTabsContent">
                        <!-- 个人资料表单 -->
                        <div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
                            <?php if (isset($error)): ?>
                                <div class="alert alert-danger"><?php echo $error; ?></div>
                            <?php endif; ?>
                            
                            <?php if (isset($_SESSION['flash_messages']['success'])): ?>
                                <div class="alert alert-success"><?php echo $_SESSION['flash_messages']['success']; ?></div>
                                <?php unset($_SESSION['flash_messages']['success']); ?>
                            <?php endif; ?>
                            
                            <form action="profile.php" method="post" enctype="multipart/form-data">
                                <?php
// 继续设置默认值
$userData['email'] = $userData['email'] ?? '';
$userData['phone'] = $userData['phone'] ?? '';
?>
                                <div class="mb-3">
                                    <label for="username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="username" value="<?php echo htmlspecialchars($userData['username']); ?>" disabled>
                                    <div class="form-text">用户名不可修改</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="email" class="form-label">邮箱</label>
                                    <input type="email" class="form-control" id="email" value="<?php echo htmlspecialchars($userData['email']); ?>" disabled>
                                    <div class="form-text">邮箱不可修改</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="phone" class="form-label">手机号</label>
                                    <input type="text" class="form-control" id="phone" value="<?php echo htmlspecialchars($userData['phone']); ?>" disabled>
                                    <div class="form-text">手机号不可修改</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="nickname" class="form-label">昵称</label>
                                    <input type="text" class="form-control" id="nickname" name="nickname" value="<?php echo htmlspecialchars($userData['nickname']); ?>" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="avatar" class="form-label">头像</label>
                                    <input type="file" class="form-control" id="avatar" name="avatar" accept="image/*">
                                    <div class="form-text">支持JPG、JPEG、PNG和GIF格式，建议尺寸为200x200像素</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="bio" class="form-label">个人简介</label>
                                    <textarea class="form-control" id="bio" name="bio" rows="3"><?php echo htmlspecialchars($userData['bio'] ?? ''); ?></textarea>
                                </div>
                                
                                <button type="submit" name="update_profile" class="btn btn-primary">保存修改</button>
                            </form>
                        </div>
                        
                        <!-- 修改密码表单 -->
                        <div class="tab-pane fade" id="password" role="tabpanel" aria-labelledby="password-tab">
                            <?php if (isset($passwordError)): ?>
                                <div class="alert alert-danger"><?php echo $passwordError; ?></div>
                            <?php endif; ?>
                            
                            <form action="profile.php" method="post">
                                <div class="mb-3">
                                    <label for="current_password" class="form-label">当前密码</label>
                                    <input type="password" class="form-control" id="current_password" name="current_password" required>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="new_password" class="form-label">新密码</label>
                                    <input type="password" class="form-control" id="new_password" name="new_password" required>
                                    <div class="form-text">密码长度至少为6个字符</div>
                                </div>
                                
                                <div class="mb-3">
                                    <label for="confirm_password" class="form-label">确认新密码</label>
                                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                                </div>
                                
                                <button type="submit" name="change_password" class="btn btn-primary">修改密码</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include '/www/wwwroot/MA.com/includes/footer.php'; ?>
